<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一对完全一样的网络套接字连接流</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.stream-socket-pair" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_pair</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">stream_socket_pair</span> &mdash; <span class="dc-title">
   创建一对完全一样的网络套接字连接流
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-socket-pair-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_socket_pair</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$domain</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$protocol</code></span>
   ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>stream_socket_pair()</strong></span> 创建一对完全一样的网络套接字连接，这个函数通常会被用在进程间通信(Inter-Process Communication) 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-pair-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">domain</code></dt>

     <dd>

      <p class="para">
       使用的协议族： <strong><code>STREAM_PF_INET</code></strong>,
       <strong><code>STREAM_PF_INET6</code></strong> or
       <strong><code>STREAM_PF_UNIX</code></strong>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">type</code></dt>

     <dd>

      <p class="para">
       通信类型:
       <strong><code>STREAM_SOCK_DGRAM</code></strong>,
       <strong><code>STREAM_SOCK_RAW</code></strong>,
       <strong><code>STREAM_SOCK_RDM</code></strong>,
       <strong><code>STREAM_SOCK_SEQPACKET</code></strong> or
       <strong><code>STREAM_SOCK_STREAM</code></strong>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">protocol</code></dt>

     <dd>

      <p class="para">
       使用的传输协议: <strong><code>STREAM_IPPROTO_ICMP</code></strong>,
       <strong><code>STREAM_IPPROTO_IP</code></strong>,
       <strong><code>STREAM_IPPROTO_RAW</code></strong>,
       <strong><code>STREAM_IPPROTO_TCP</code></strong> or
       <strong><code>STREAM_IPPROTO_UDP</code></strong>       
      </p>
     </dd>

    
   </dl>

  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Please consult the <a href="stream.constants.html" class="link">Streams constant
     list</a> for further details on each constant.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-pair-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果成功将返回一个<span class="type"><span class="type 数组">数组</span></span>包括了两个socket资源，错误时返回<strong><code>FALSE</code></strong>
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-socket-pair-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        这个函数在windows平台不可用
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-socket-pair-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4839">
    <p><strong>Example #1 A <span class="function"><strong>stream_socket_pair()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     This example shows the basic usage of
     <span class="function"><strong>stream_socket_pair()</strong></span> in Inter-Process Comunication.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$sockets&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_pair</span><span style="color: #007700">(</span><span style="color: #0000BB">STREAM_PF_UNIX</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_IPPROTO_IP</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">$pid&nbsp;</span><span style="color: #007700">==&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'could&nbsp;not&nbsp;fork'</span><span style="color: #007700">);<br /><br />}&nbsp;else&nbsp;if&nbsp;(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;parent&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"child&nbsp;PID:&nbsp;</span><span style="color: #0000BB">$pid</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;child&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"message&nbsp;from&nbsp;child\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
child PID: 1378
message from child
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>